﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GoPlay.Models;

namespace GoPlay.Controllers
{
    [Authorize]
    public class PlayersController : Controller
    {
        IRepository<Player> _repository;
        IRepository<Contact> _contactsRepository;

        public PlayersController() : this(new EFRepository<Player>()) 
        {
            _contactsRepository = new EFRepository<Contact>();
        }

        public PlayersController(IRepository<Player> repository)
        {
            _repository = repository;
            _contactsRepository = new InMemoryRepository<Contact>();
        }


        //
        // GET: /Players/Create

        public ActionResult Create()
        {
            var teamId = RouteData.Values["TeamId"].ToString();
            ContactsController contactsController = new ContactsController(_contactsRepository);
            var userId = Convert.ToInt32(Session["UserId"]);
            var contacts = _contactsRepository.GetAllByUserId(userId);
            
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var c in contacts)
            {
                items.Add(new SelectListItem { Text = c.Name, Value = c.Id.ToString() });
            }

            var player = new Player();
            ViewBag.Contacts = items;
            return View(player);
        }

        //
        // POST: /Players/Create

        [HttpPost]
        public ActionResult Create(Player player)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.Insert(player);
                    _repository.SaveChanges();
                    Session["FlashSuccess"] = "Your player was added.";
                    //return RedirectToAction("Details", "Teams", new { id = RouteData.Values["TeamId"].ToString() }); //("Index");
                    return RedirectToAction("Details", "Teams", new { id = player.TeamId }); //("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View(player);
        }

        //
        // GET: /Players/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Player c = _repository.GetByID(id);
            if (c == null)
            {
                return HttpNotFound();
            }

            var teamId = RouteData.Values["TeamId"].ToString();
            ContactsController contactsController = new ContactsController(_contactsRepository);
            var userId = Convert.ToInt32(Session["UserId"]);
            var contacts = _contactsRepository.GetAllByUserId(userId);

            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var o in contacts)
            {
                items.Add(new SelectListItem { Text = o.Name, Value = o.Id.ToString() });
            }
            ViewBag.Contacts = items;
            return View(c);
        }

        //
        // POST: /Players/Edit/5

        [HttpPost]
        public ActionResult Edit(Player player)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.Update(player);
                    _repository.SaveChanges();
                    Session["FlashSuccess"] = "Your player was updated.";
                    return RedirectToAction("Details", "Teams", new { id = RouteData.Values["TeamId"].ToString() });
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View(player);
        }

        //
        // GET: /Players/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Player c = _repository.GetByID(id);
            if (c == null)
            {
                return HttpNotFound();
            }
            return View(c);
        }

        //
        // POST: /Players/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {

            try
            {
                Player player = _repository.GetByID(id);
                _repository.Delete(player);
                _repository.SaveChanges();
                Session["FlashSuccess"] = "Your player was deleted.";
                return RedirectToAction("Details", "Teams", new { id = RouteData.Values["TeamId"].ToString() }); //("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                Session["FlashError"] = ex.Message;
            }

            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    Session["FlashError"] += error.ErrorMessage;
                }
            }
            return View();
        }
    }
}